import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:sptapp/api/interface/user_api.dart';
import 'package:sptapp/api/request/response_data.dart';
import 'package:sptapp/widget/common_widget.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../config/const.dart';
import '../../moudle/level_picker/level_picker.dart';
import '../../moudle/message.dart';

class ConnectUsPage extends StatefulWidget {
  const ConnectUsPage({super.key});

  @override
  State<ConnectUsPage> createState() => _ConnectUsPageState();
}

class _ConnectUsPageState extends State<ConnectUsPage> {

  String? account;
  String? phone;
  String? content;
  String? type;
  bool isDone = false;

  Map feedbackInfo= {
    'spt.receive.email':{
      // 'value':'api@sptus.com'
      'value':''
    },
    'spt.phone':{
      // 'value':'800-677-5014'
      'value':''
    },
  };

  TextEditingController accountController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController contentController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getFeedbackInfo();
  }

  _getFeedbackInfo()async{
    ResponseData res = await UserAPI.getFeedbackInfo();
    if(res.code == 200 && res.data != null){
      if(mounted){
        setState(() {
          feedbackInfo = res.data;
        });
      }
    }
  }
  _sure()async{
    Map<String,dynamic> data = {
      'msgType': type,
      'name':account,
      'contactWay':phone,
      'msg':content
    };
    showLoading();
    ResponseData res = await UserAPI.problemFeedback(data);
    hideLoading();
    if(res.code == 200){
      showToast(connect_us_page_item_submit_success.tr);
      Navigator.of(context).pop();
    }
  }

  getIsDone(){
    if(type == null || type!.isEmpty || account == null || account!.isEmpty || phone == null || phone!.isEmpty || content == null || content!.isEmpty ){
      isDone = false;
    }else{
      isDone = true;
    }
    if(mounted){
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 680.w,
      padding: EdgeInsets.only(left: 48.w,right: 48.w,top: 32.w,bottom: 48.w),
      decoration: BoxDecoration(
          color: Color(0xff1E1E1E),
          borderRadius: BorderRadius.only(topLeft: Radius.circular(32.w),topRight: Radius.circular(32.w))
      ),
      child: Column(
        children: [
          Container(
            child: Row(
              children: [
                Text(min_connect_us_text.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
                GestureDetector(
                  onTap: (){

                  },
                  child: Icon(Icons.close,size: 30.sp,color: Color(0xff999999),),
                )
              ],
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
            ),
          ),
          SizedBox(height: 100.w,),
          GestureDetector(
            onTap: (){
              SmartDialog.dismiss();
              launchUrl(Uri.parse((feedbackInfo['spt.phone']['value']??'800-677-5014').toString().split("-").join("")));
            },
            child: Container(
              height: 150.w,
              child: Stack(
                children: [
                  Image.asset('assets/home/min_connect_us_phone.png',height: 150.w,fit: BoxFit.fitHeight,),
                  Container(
                    width: 670.w,
                    height: 150.w,
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.only(left: 48.w),
                    child: Text(connect_us_phone_text.tr,style: TextStyle(
                        color: Colors.white,
                        fontSize: 28.sp
                    ),),
                  )
                ],
              ),
            ),
          ),
          SizedBox(height: 32.w,),
          GestureDetector(
            onTap: (){
              SmartDialog.dismiss();
              launchUrl(Uri.parse('sms:'+(feedbackInfo['spt.receive.email']['value']??'api@sptus.com')));
            },
            child: Container(
              child: Stack(
                children: [
                  Image.asset('assets/home/min_connect_us_mail.png',width: 670.w,fit: BoxFit.fitWidth,),
                  Container(
                    width: 670.w,
                    height: 150.w,
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.only(left: 48.w),
                    child: Text(connect_us_mail_text.tr,style: TextStyle(
                        color: Colors.white,
                        fontSize: 28.sp
                    ),),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
    return Scaffold(
      backgroundColor: defaultBgColor,
      appBar: myAppBar(context, connect_us_page_title.tr),
      body: GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: SingleChildScrollView(
          child: Container(
            alignment: Alignment.center,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(height: 20.w,),
                //用户名
                Container(
                  child: Column(
                    children: [
                      Container(
                        width: 690.w,
                        child: Text(connect_us_page_item_phone.tr),
                        alignment: Alignment.centerLeft,
                      ),
                      Container(
                        width: 690.w,
                        height: 100.w,
                        padding: EdgeInsets.symmetric(horizontal: 20.w),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            // borderRadius: BorderRadius.circular(100.w)
                        ),
                        child:  Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Expanded(child: Text(feedbackInfo['spt.phone']['value']??'800-677-5014',style: TextStyle(
                                color: inputColor,
                                fontSize: 32.sp,
                                fontWeight: FontWeight.w600
                            ),)),
                            SizedBox(width: 20.w,),
                            GestureDetector(
                              onTap: (){
                              },
                              child: Visibility(
                                child: GestureDetector(
                                  onTap: (){
                                    launchUrl(Uri.parse((feedbackInfo['spt.phone']['value']??'800-677-5014').toString().split("-").join("")));
                                  },
                                  child: Container(
                                    child: Icon(Icons.phone,size: 40.w,),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                SizedBox(height: 40.w,),
                Container(
                  child: Column(
                    children: [
                      Container(
                        width: 690.w,
                        child: Text(connect_us_page_item_email.tr),
                        alignment: Alignment.centerLeft,
                      ),
                      Container(
                        width: 690.w,
                        height: 100.w,
                        padding: EdgeInsets.symmetric(horizontal: 20.w),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          // borderRadius: BorderRadius.circular(100.w)
                        ),
                        child:  Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Expanded(child: Text(feedbackInfo['spt.receive.email']['value']??'api@sptus.com',style: TextStyle(
                                color: inputColor,
                                fontSize: 32.sp,
                                fontWeight: FontWeight.w600
                            ),)),
                            SizedBox(width: 20.w,),
                            GestureDetector(
                              onTap: (){
                              },
                              child: Visibility(
                                child: GestureDetector(
                                  onTap: ()async{
                                    // await Clipboard.setData(ClipboardData(text: feedbackInfo['spt.receive.email']['value']??'api@sptus.com'));
                                    // showToast(connect_us_page_item_copy_success.tr);
                                    launchUrl(Uri.parse('sms:'+(feedbackInfo['spt.receive.email']['value']??'api@sptus.com')));
                                  },
                                  child: Container(
                                    child: Icon(Icons.mail,size: 40.w,),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                // SizedBox(height: 40.w,),
                // Container(
                //   child: Column(
                //     children: [
                //       Container(
                //         width: 690.w,
                //         child: Text(connect_us_page_item_type.tr),
                //         alignment: Alignment.centerLeft,
                //       ),
                //       Container(
                //         width: 690.w,
                //         height: 100.w,
                //         padding: EdgeInsets.symmetric(horizontal: 20.w),
                //         decoration: BoxDecoration(
                //           color: Colors.white,
                //           // borderRadius: BorderRadius.circular(100.w)
                //         ),
                //         child:  Row(
                //           crossAxisAlignment: CrossAxisAlignment.center,
                //           children: [
                //             Expanded(child: Text(type??'',style: TextStyle(
                //                 color: inputColor,
                //                 fontSize: 32.sp,
                //                 fontWeight: FontWeight.w600
                //             ),)),
                //             SizedBox(width: 20.w,),
                //             GestureDetector(
                //               onTap: (){
                //               },
                //               child: Visibility(
                //                 child: GestureDetector(
                //                   onTap: ()async{
                //                     FocusScope.of(context).requestFocus(FocusNode());
                //                     JhPickerTool.showStringPicker(context,
                //                         data: [connect_us_page_item_type_question.tr,connect_us_page_item_type_business.tr],
                //                         clickCallBack: _selectType,
                //                         normalIndex: 0);
                //                   },
                //                   child: Container(
                //                     child: Icon(Icons.arrow_drop_down_outlined,size: 40.w,),
                //                   ),
                //                 ),
                //               ),
                //             )
                //           ],
                //         ),
                //       )
                //     ],
                //   ),
                // ),
                // SizedBox(height: 40.w,),
                // Container(
                //   child: Column(
                //     children: [
                //       Container(
                //         width: 690.w,
                //         child: Text(connect_us_page_item_user_name.tr),
                //         alignment: Alignment.centerLeft,
                //       ),
                //       Container(
                //         width: 690.w,
                //         height: 100.w,
                //         decoration: BoxDecoration(
                //             color: Colors.white,
                //             borderRadius: BorderRadius.circular(100.w)
                //         ),
                //         child:  Row(
                //           crossAxisAlignment: CrossAxisAlignment.center,
                //           children: [
                //             SizedBox(width: 20.w,),
                //             Expanded(child: TextField(
                //               //控制器
                //               textInputAction: TextInputAction.done,
                //               controller: accountController,
                //               maxLength: 20,
                //               maxLines: 1,
                //               //是否自动更正
                //               autocorrect: true,
                //               //是否自动对焦
                //               autofocus: false,
                //               style: TextStyle(
                //                   color: inputColor,
                //                   fontSize: 32.sp,
                //                   fontWeight: FontWeight.w600
                //               ),
                //               inputFormatters: [
                //
                //               ],
                //               decoration: InputDecoration(
                //                 contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                //                 // hintText: user_name_hinText.tr,
                //                 hintStyle: TextStyle(
                //                     color: inputColor,
                //                     fontSize: 32.sp,
                //                     fontWeight: FontWeight.w600
                //                 ),
                //                 counterText: "",
                //                 border: OutlineInputBorder(
                //                     borderSide: BorderSide.none),
                //               ),
                //               onChanged: (val){
                //                 setState(() {
                //                   account = val.trim();
                //                 });
                //                 getIsDone();
                //               },
                //             )),
                //             SizedBox(width: 20.w,),
                //             Visibility(visible: account!=null&&account!.isNotEmpty,
                //               child: GestureDetector(
                //                 onTap: (){
                //                   setState(() {
                //                     accountController.text = '';
                //                     account = null;
                //                   });
                //                   getIsDone();
                //                 },
                //                 child: Container(
                //                   child: Image.asset('assets/login/icon_clear.png',width: 56.w,),
                //                 ),
                //               ),
                //             )
                //           ],
                //         ),
                //       )
                //     ],
                //   ),
                // ),
                // SizedBox(height: 40.w,),
                // Container(
                //   child: Column(
                //     children: [
                //       Container(
                //         width: 690.w,
                //         child: Text(connect_us_page_item_user_phone.tr),
                //         alignment: Alignment.centerLeft,
                //       ),
                //       Container(
                //         width: 690.w,
                //         height: 100.w,
                //         decoration: BoxDecoration(
                //             color: Colors.white,
                //             borderRadius: BorderRadius.circular(100.w)
                //         ),
                //         child:  Row(
                //           crossAxisAlignment: CrossAxisAlignment.center,
                //           children: [
                //             SizedBox(width: 20.w,),
                //             Expanded(child: TextField(
                //               //控制器
                //               textInputAction: TextInputAction.done,
                //               controller: phoneController,
                //               maxLength: 20,
                //               maxLines: 1,
                //               //是否自动更正
                //               autocorrect: true,
                //               //是否自动对焦
                //               autofocus: false,
                //               style: TextStyle(
                //                   color: inputColor,
                //                   fontSize: 32.sp,
                //                   fontWeight: FontWeight.w600
                //               ),
                //               inputFormatters: [
                //               ],
                //               decoration: InputDecoration(
                //                 contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                //                 // hintText: user_name_hinText.tr,
                //                 hintStyle: TextStyle(
                //                     color: inputColor,
                //                     fontSize: 32.sp,
                //                     fontWeight: FontWeight.w600
                //                 ),
                //                 counterText: "",
                //                 border: OutlineInputBorder(
                //                     borderSide: BorderSide.none),
                //               ),
                //               onChanged: (val){
                //                 setState(() {
                //                   phone = val.trim();
                //                 });
                //                 getIsDone();
                //               },
                //             )),
                //             SizedBox(width: 20.w,),
                //             Visibility(visible: phone!=null&&phone!.isNotEmpty,
                //               child: GestureDetector(
                //                 onTap: (){
                //                   setState(() {
                //                     phoneController.text = '';
                //                     phone = null;
                //                   });
                //                   getIsDone();
                //                 },
                //                 child: Container(
                //                   child: Image.asset('assets/login/icon_clear.png',width: 56.w,),
                //                 ),
                //               ),
                //             )
                //           ],
                //         ),
                //       )
                //     ],
                //   ),
                // ),
                // SizedBox(height: 40.w,),
                // Container(
                //   child: Column(
                //     children: [
                //       Container(
                //         width: 690.w,
                //         child: Text(connect_us_page_item_content.tr),
                //         alignment: Alignment.centerLeft,
                //       ),
                //       Container(
                //         width: 690.w,
                //         height: 200.w,
                //         decoration: BoxDecoration(
                //             color: Colors.white,
                //             borderRadius: BorderRadius.circular(20.w)
                //         ),
                //         child:  Row(
                //           crossAxisAlignment: CrossAxisAlignment.center,
                //           children: [
                //             SizedBox(width: 20.w,),
                //             Expanded(child: TextField(
                //               //控制器
                //               controller: contentController,
                //               maxLength: 20,
                //               maxLines: null,
                //               //是否自动更正
                //               autocorrect: true,
                //               //是否自动对焦
                //               autofocus: false,
                //               style: TextStyle(
                //                   color: inputColor,
                //                   fontSize: 32.sp,
                //                   fontWeight: FontWeight.w600
                //               ),
                //               inputFormatters: [
                //
                //               ],
                //               decoration: InputDecoration(
                //                 contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                //                 // hintText: user_name_hinText.tr,
                //                 hintStyle: TextStyle(
                //                     color: inputColor,
                //                     fontSize: 32.sp,
                //                     fontWeight: FontWeight.w600
                //                 ),
                //                 counterText: "",
                //                 border: OutlineInputBorder(
                //                     borderSide: BorderSide.none),
                //               ),
                //               onChanged: (val){
                //                 setState(() {
                //                   content = val.trim();
                //                 });
                //                 getIsDone();
                //               },
                //             )),
                //             SizedBox(width: 20.w,),
                //             Visibility(visible: content!=null&&content!.isNotEmpty,
                //               child: GestureDetector(
                //                 onTap: (){
                //                   setState(() {
                //                     contentController.text = '';
                //                     content = null;
                //                   });
                //                   getIsDone();
                //                 },
                //                 child: Container(
                //                   child: Image.asset('assets/login/icon_clear.png',width: 56.w,),
                //                 ),
                //               ),
                //             )
                //           ],
                //         ),
                //       )
                //     ],
                //   ),
                // ),
                // SizedBox(height: 80.w,),
                // GestureDetector(
                //   onTap: () {
                //     FocusScope.of(context).requestFocus(FocusNode());
                //     getIsDone();
                //     if(isDone){
                //       _sure();
                //     }
                //   },
                //   child: Container(
                //     height: 100.w,
                //     width: 690.w,
                //     alignment: Alignment.center,
                //     decoration: BoxDecoration(
                //         color: isDone?themeColor:Colors.grey,
                //         borderRadius: BorderRadius.circular(100.w)
                //     ),
                //     child: Text(connect_us_page_item_submit.tr,style: TextStyle(
                //         color: Colors.white,
                //         fontSize: 28.sp,
                //         fontWeight: FontWeight.w600
                //     ),),
                //   ),
                // ),

              ],
            ),
          ),
        ),
      ),
    );
  }

  _selectType(int index, dynamic selectDate) {
    setState(() {
      if(index == 0){
        type = connect_us_page_item_type_question.tr;
      }else{
        type = connect_us_page_item_type_business.tr;
      }
    });
    getIsDone();
  }
}
